﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;

namespace ProjScan.Tools
{
    //
    // 摘要:
    //     扫描插件相关信息 - ScanInfo - Class154
    internal class ScanInfo : Singleton<ScanInfo>
    {
        public enum Platform
        {
            UnixLike,
            Windows
        }

        public int[] unityVersion = new int[2];

        public bool isBatchMode = false;

        public bool isNoGraphics = false;

        public bool isQuit = false;

        public bool isScanQuit = false;

        public bool isRunning = true;

        public bool isEditor = true;

        public string version = GetScanVer();

        public GraphicsDeviceType graphicsDeviceType;

        public Platform platform;

        public void Init()
        {
            string[] commandLineArgs = Environment.GetCommandLineArgs();
            HashSet<string> hashSet = new HashSet<string>(commandLineArgs);
            UnityEngine.Debug.Log("GetCmdLineArgs: " + string.Join(", ", commandLineArgs));
            isBatchMode = hashSet.Contains("-batchmode");
            isQuit = hashSet.Contains("-quit");
            isScanQuit = hashSet.Contains("-scanquit");
            isNoGraphics = hashSet.Contains("-nographics");
            platform = GetPlatform();
            GetUnityVersion();
            graphicsDeviceType = SystemInfo.graphicsDeviceType;
            isEditor = (!isBatchMode && !isNoGraphics);
            isRunning = (!isBatchMode && !isQuit && !isNoGraphics);
            ScanLog.Instance.LogInfo("Version: " + version);
            ScanLog.Instance.LogInfo("Batchmode: " + isBatchMode);
            ScanLog.Instance.LogInfo("NoGraphics: " + isNoGraphics);
            ScanLog.Instance.LogInfo("isScanQuit: " + isScanQuit);
            ScanLog.UnityLog($"Version: {version}, Batchmode: {isBatchMode}, NoGraphics: {isNoGraphics}, isScanQuit: {isScanQuit}");
        }

        private Platform GetPlatform()
        {
            switch (Environment.OSVersion.Platform)
            {
                case PlatformID.Win32S:
                case PlatformID.Win32Windows:
                case PlatformID.Win32NT:
                case PlatformID.WinCE:
                    return Platform.Windows;
                case PlatformID.Unix:
                case PlatformID.MacOSX:
                    return Platform.UnixLike;
                default:
                    UnityEngine.Debug.LogError("Failed to determine OS type.");
                    throw new Exception("Proj Scan: Failed to determine OS type.");
            }
        }

        private void GetUnityVersion()
        {
            try
            {
                char[] separator = new char[1]
                {
                    '.'
                };
                unityVersion[0] = int.Parse(Application.unityVersion.Split(separator)[0]);
                unityVersion[1] = int.Parse(Application.unityVersion.Split(separator)[1]);
                ScanLog.Instance.LogInfo("UnityVersionInfo " + unityVersion[0] + "." + unityVersion[1]);
            }
            catch (Exception ex)
            {
                UnityEngine.Debug.LogError("Failed to parse unity version info: " + ex.Message + ex.StackTrace);
                UnityEngine.Debug.LogError("Exception type: " + ex.GetType()?.ToString() + "\n" + ex.Message + "\n" + ex.StackTrace);
            }
        }
 

        public static string GetScanVer()
        {
            Version version = Assembly.GetExecutingAssembly().GetName().Version;
            string[] values = new string[7]
            {
                version.Major.ToString(),
                ".",
                version.Minor.ToString(),
                ".",
                version.Build.ToString(),
                ".",
                version.Revision.ToString()
            };
            return string.Concat(values);
        }
    }
}